#include "astree.h"

#ifdef TRE_DBG
#define dbg(...)		printf("%s: ", __func__), printf(__VA_ARGS__)
#else
#define dbg(...)		
#endif

tree_node_t *c_node(char *name, tree_node_t **new_child) {
	dbg("Creating Node: %s\n", name);
	tree_node_t *node = malloc(sizeof(tree_node_t));

	node->type = NONTERMINAL;
	node->op_type = NONE;
	node->name = name;
	node->burm_child[0] = NULL;
	node->burm_child[1] = NULL;

	for(int i=0; i<MC; i++) {
		(node->data.child)[i] = new_child[i];
	}
	return node;
}

tree_node_t *c_lex(char lexem) {
	dbg("Creating lexem Node: %c\n", lexem);
	tree_node_t *node = malloc(sizeof(tree_node_t));

	node->data.lexem = lexem;
	node->type = TERMINAL_LEX;
	node->op_type = NONE;
	node->name = "LEXEM";
	node->burm_child[0] = NULL;
	node->burm_child[1] = NULL;
	return node;
}

tree_node_t *c_tok(int token) {
	dbg("Creating token Node: ");
	switch(token) {
		case END:
			dbg("END\n");
			break;
		case RETURN:
			dbg("RETURN\n");
			break;
		case GOTO:
			dbg("GOTO\n");
			break;
		case IF:
			dbg("IF\n");
			break;
		case VAR:
			dbg("VAR\n");
			break;
		case AND:
			dbg("AND\n");
			break;
		case NOT:
			dbg("NOT\n");
			break;
		case NEQ:
			dbg("NEQ\n");
			break;
		default:
			dbg("ERROR\n");
			break;
	}

	tree_node_t *node = malloc(sizeof(tree_node_t));

	node->data.token = token;
	node->type = TERMINAL_TOK;
	node->op_type = NONE;
	node->name = "TOKEN";
	node->burm_child[0] = NULL;
	node->burm_child[1] = NULL;
	return node;
}

tree_node_t *c_id(id_syntax_cb_t syntax_callback, char* name, long line, symbolt_t type) {
	dbg("Creating id Node: %s %ld\n", name, line);
	tree_node_t *node = malloc(sizeof(tree_node_t));

	node->data.id.name = name;
	node->data.id.line =  line;
	node->data.id.type = type;
	node->data.id.syntax_cb = syntax_callback;
	node->type = TERMINAL_ID;
	node->op_type = NONE;
	node->name = "ID";
	node->burm_child[0] = NULL;
	node->burm_child[1] = NULL;
	return node;
}

tree_node_t *c_num(long val, long line) {
	dbg("Creating num Node: %ld %ld\n", val, line);
	tree_node_t *node = malloc(sizeof(tree_node_t));

	node->data.num.val = val;
	node->data.num.line =  line;
	node->type = TERMINAL_NUM;
	node->op_type = NONE;
	node->name = "NUM";
	node->burm_child[0] = NULL;
	node->burm_child[1] = NULL;
	return node;
}
